
package com.bgs.engineering.mybatis;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.io.File;
import java.util.*;


public class GsGenerator {
  static final String username = "root1";
  static final String password = "15830237076Sy";
  static final String url = "jdbc:mysql://rm-cn-pe339rk520007cmo.rwlb.rds.aliyuncs.com:3306/engineering?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true";

  public static void main(String[] args) {
    File file = new File("");
    String path = file.getAbsolutePath();
    Scanner scanner1 = new Scanner(System.in);
    System.out.println("输入模块名,没有输入无");
    String td = "/" + scanner1.nextLine();
    if ("/无".equals(td)) {
      td = "";
    }
    String finalTd = td;
    String pathXml = path + "/src/main/resources/mapper";
    FastAutoGenerator.create(url, username, password)
            .globalConfig((scanner, builder) ->
                    builder.outputDir(path  + "\\src\\main\\java")
                            .author(scanner.apply("输入作者!"))
                            .disableOpenDir()
                            .fileOverride()
                            .dateType(DateType.ONLY_DATE)
                            .commentDate("yyyy-MM-dd HH:mm:ss")
            )
            .packageConfig((scanner, builder) -> builder.parent("com.bgs.engineering.api" + finalTd.replace("/", "."))//
                    .pathInfo(new HashMap<OutputFile, String>() {{
                      put(OutputFile.mapperXml, pathXml);
                      put(OutputFile.entity, path + "/src/main/java/com/bgs/engineering/api" + finalTd + "/entity");
                    }})
            )
            .strategyConfig((scanner, builder) -> {
              builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all!")));
              builder.entityBuilder().enableLombok().addTableFills(new Column("create_time", FieldFill.INSERT))
                      .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                      .logicDeleteColumnName("delete_flag")
              ;
              builder.mapperBuilder().enableMapperAnnotation().build();
              builder.controllerBuilder().enableHyphenStyle().enableRestStyle();
              builder.serviceBuilder().formatServiceFileName("%sService")
                      .formatServiceImplFileName("%sServiceImpl");
            })
            .execute();

  }

  private static List<String> getTables(String tables) {
    return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
  }
}
